﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Timers;

namespace OTDR_OPT
{
    /*
     横河组合仪类,通过它实现控制光开关、衰减器、读取光功率值
         */
    public class Yokogawa:IOpticalPowerMeter,IOpticalSwitch,IAttenuation
    {
        private AQ2211 connectionParam = null;
        private Socket connectionSockect = null;
        private Timer checkConnectTimer = new Timer(1000);  //检查是否连接的定时器

        #region 事件
        public event EventHandler<EventArgs> ConnnectBreaked; //当连接后异常断开，发生事件
        #endregion


        #region 单例模式
        private static Yokogawa yokogawa = null;

        private Yokogawa() {
            checkConnectTimer.Elapsed += new ElapsedEventHandler(CheckConnectState); //定时检测连接是否断开
        }

        public static Yokogawa GetInstance(AQ2211 aq2211)
        {
            if (
                yokogawa == null) { yokogawa = new Yokogawa();
                yokogawa.connectionParam = aq2211;
            }
            return yokogawa;
        }

        #endregion


        //连接设备
        public bool Connect()
        {
            string ipAddress = connectionParam.IpAddress;
            int portNumber = Convert.ToInt32(connectionParam.PortNum);
            if (ipAddress == null || ipAddress == "")
            {
                return false;
            }
            try
            {
                connectionSockect = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress address = IPAddress.Parse(ipAddress);
                IPEndPoint endPoint = new IPEndPoint(address,portNumber);
                connectionSockect.Connect(endPoint);
                return true;
            }
            catch (Exception) { return false; }
        }


        //断开连接j
        public void DisConnect()
        {
            checkConnectTimer.Stop();
            connectionSockect.Close();
        }


        //定时检测连接状态
        private void CheckConnectState(object sender, ElapsedEventArgs arg)
        {
            try
            {
                byte[] tmp = new byte[1];
                connectionSockect.Send(tmp, 0, 0);
            }
            catch (SocketException e)
            {
                if (e.NativeErrorCode.Equals(10035))
                {
                    return;
                }
                ConnnectBreaked?.Invoke(this, new EventArgs());  //如果连接异常，则产生一个事件
            }
        }


        //修改波长(含光功、光衰)
        public bool ChangeWavelength(string wavelength)
        {
            throw new NotImplementedException();
        }


        //读取光功率值
        public double ReadPower()
        {
            throw new NotImplementedException();
        }


        //切换光开关通道
        public bool SwitchCannel(int channel)
        {
            throw new NotImplementedException();
        }


        //修改衰减值
        public bool ChangeAttenuation(double attenuation)
        {
            throw new NotImplementedException();
        }
    }
}
